package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021-03-23 14:05
 * @Version 1.0
 * 考察递归
 **/
public class FlattenNestedListIterator {



}

class NestedIterator implements Iterator<Integer> {

    private List<NestedInteger> nestedList;
    private List<Integer> keys;
    private int tag;
    private int size;

    public NestedIterator(List<NestedInteger> nestedList) {
        this.nestedList = nestedList;
        this.tag = 0;
        this.keys = new ArrayList<>();
        if (nestedList != null && !nestedList.isEmpty()) {
            int len = nestedList.size();
            int index = 0;
            initKeys(nestedList,index, len);
        }
        this.size = this.keys.size();
    }

    private void initKeys(List<NestedInteger> nestedList,int index, int len) {
        if (index == len || nestedList.isEmpty()) {
            return;
        }
        NestedInteger nestedInteger = nestedList.get(index);
        if (nestedInteger.isInteger()) {
            this.keys.add(nestedInteger.getInteger());
        } else {
            List<NestedInteger> list = nestedInteger.getList();
            initKeys(list,0,list.size());
        }
        initKeys(nestedList,index+1,len);
    }

    @Override
    public Integer next() {
        Integer result = this.keys.get(tag);
        tag++;
        return result;
    }

    @Override
    public boolean hasNext() {
        return tag != size;
    }
}

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
interface NestedInteger {
    // @return true if this NestedInteger holds a single integer, rather than a nested lis
    public boolean isInteger();
    // @return the single integer that this NestedInteger holds, if it holds a single inte
    // Return null if this NestedInteger holds a nested list
    public Integer getInteger();
    // @return the nested list that this NestedInteger holds, if it holds a nested list
    // Return null if this NestedInteger holds a single integer
    public List<NestedInteger> getList();
}